var express = require('express');
var router = express.Router();
var Mock=require('mockjs')
const crypto = require("crypto"); //引入加密模块
var {
  userModel,
  userinfoModel,
  addressModel,
  areaModel,
  hospitalModel,
  departmentClassModel,
  departmentSubjectModel,
  orderDetailModel,
  evaluateModel,
  commonModel,
  AdmissionsModel,
  medicalModel,
  departmentModel,
  categoryModel,
  drugModel,
  frequencyModel,
  onceModel,
  methodModel,
  articleModel,
  draftModel,
  topicModel,
  messageModel,
  groupModel
}=require('../model/model');
const { default: mongoose } = require('mongoose');
/* GET home page. */
router.post('/adduser',(req,res)=>{
  userModel.create(req.body)
  res.send({
    code:200,
    msg:'用户添加成功'
  })
})

router.post('/adduserinfo',(req,res)=>{
  userinfoModel.create(req.body)
  res.send({
    code:200,
    msg:'用户信息添加成功'
  })
})

router.post('/addaddress',(req,res)=>{
  let List=[]
  for(let i=0;i<10;i++){
    let data=Mock.mock({
      'name':'@city',//城市
    })
    List.push(data)
  }
  addressModel.create(List)
  res.send({
    code:200,
    msg:'地址添加成功'
  })
})

router.post('/addarea',(req,res)=>{
  areaModel.create(req.body)
  res.send({
    code:200,
    msg:'区域添加成功'
  })
})

router.post('/addhospital',(req,res)=>{
  hospitalModel.create(req.body)
  res.send({
    code:200,
    msg:'医院添加成功'
  })
})

router.post('/adddepartmentClass',(req,res)=>{
  departmentClassModel.create(req.body)
  res.send({
    code:200,
    msg:'科室分类添加成功'
  })
})

router.post('/adddepartmentSubject',(req,res)=>{
  departmentSubjectModel.create(req.body)
  res.send({
    code:200,
    msg:'科室科目添加成功'
  })
})

router.post('/addorderDetail',(req,res)=>{
  orderDetailModel.create(req.body)
  res.send({
    code:200,
    msg:'订单明细表添加成功'
  })
})

router.post('/addevaluate',(req,res)=>{
  evaluateModel.create(req.body)
  res.send({
    code:200,
    msg:'评价表添加成功'
  })
})

router.post('/addcommon',(req,res)=>{
  commonModel.create(req.body)
  res.send({
    code:200,
    msg:'常用语表添加成功'
  })
})

router.post('/addAdmissions',(req,res)=>{
  AdmissionsModel.create(req.body)
  res.send({
    code:200,
    msg:'患者列表添加成功'
  })
})

router.post('/addmedical',(req,res)=>{
  medicalModel.create(req.body)
  res.send({
    code:200,
    msg:'处方列表添加成功'
  })
})

router.post('/adddepartment',(req,res)=>{
  departmentModel.create(req.body)
  res.send({
    code:200,
    msg:'药品科室列表添加成功'
  })
})

router.post('/addcategory',(req,res)=>{
  categoryModel.create(req.body)
  res.send({
    code:200,
    msg:'药品分类列表添加成功'
  })
})

router.post('/adddrug',(req,res)=>{
  drugModel.create(req.body)
  res.send({
    code:200,
    msg:'药品列表添加成功'
  })
})

router.post('/addfrequency',(req,res)=>{
  frequencyModel.create(req.body)
  res.send({
    code:200,
    msg:'用药频次表添加成功'
  })
})

router.post('/addonce',(req,res)=>{
  onceModel.create(req.body)
  res.send({
    code:200,
    msg:'单次用药表添加成功'
  })
})

router.post('/addmethod',(req,res)=>{
  methodModel.create(req.body)
  res.send({
    code:200,
    msg:'用药方法表添加成功'
  })
})

router.post('/addarticle',(req,res)=>{
  articleModel.create(req.body)
  res.send({
    code:200,
    msg:'创作中心列表添加成功'
  })
})

router.post('/adddraft',(req,res)=>{
  draftModel.create(req.body)
  res.send({
    code:200,
    msg:'草稿箱列表添加成功'
  })
})

router.post('/addtopic',(req,res)=>{
  topicModel.create(req.body)
  res.send({
    code:200,
    msg:'话题列表添加成功'
  })
})

router.post('/addmessage',(req,res)=>{
  messageModel.create(req.body)
  res.send({
    code:200,
    msg:'群发消息历史列表添加成功'
  })
})

router.post('/addgroup',(req,res)=>{
  groupModel.create(req.body)
  res.send({
    code:200,
    msg:'患者分组表单添加成功'
  })
})

//加密密码
function md5Password(password){
  const md5=crypto.createHash('md5')
  return md5.update(password).digest('hex')
}
// 登录
router.post('/login',async(req,res)=>{
  let {username,password}=req.body
  let enycryptedPassword=md5Password(password)
  const user=await userModel.find({username,password:enycryptedPassword})
  const data=await userModel.find({username,password:enycryptedPassword})
  console.log(user);
  if(user.length>0){
    res.send({
      code:200,
      msg:'登录成功',
      data:data[0]
    })
  }else{
    res.send({
      code:400,
      msg:'用户名或密码错误'
    })
  }
})
//修改密码
router.post('/updatepassword',async(req,res)=>{
  let {_id,password}=req.body
  console.log(_id);
  let enycryptedPassword=md5Password(password)
  await userModel.updateOne({_id},{password:enycryptedPassword})
  res.send({
    code:200,
    msg:'密码修改成功'
  })
})
//注册
router.post('/register',async(req,res)=>{
  let {username,password}=req.body
  let enycryptedPassword=md5Password(password)
  let data=await userModel.find({username})
  if(data.length>0){
    res.send({
      code:400,
      msg:'用户名已存在'
    })
  }else{
    await userModel.create({username,password:enycryptedPassword})
    res.send({
      code:200,
      msg:'注册成功'
    })
  }
})

//删除订单
router.post('/delorder',async(req,res)=>{
  let {_id}=req.body
  await orderDetailModel.deleteOne({_id})
  res.send({
    code:200,
    msg:'订单删除成功'
  })
})
//展示订单
router.get('/order',async(req,res)=>{
  let data=await orderDetailModel.find()
  res.send({
    code:200,
    msg:'订单展示成功',
    data
  })
})
//查找订单
router.get('/findorder',async(req,res)=>{
  let {name}=req.query
  let newname=new RegExp(name)
  let data=await orderDetailModel.find({name:newname})
  res.send({
    code:200,
    msg:'订单查找成功',
    data
  })
})
router.get('/findorder2',async(req,res)=>{
  let {_id}=req.query
  console.log(_id);
  let data=await orderDetailModel.find({_id})
  res.send({
    code:200,
    msg:'订单查找成功',
    data:data[0]
  })
})
//修改订单
router.post('/updateorder',async(req,res)=>{
  let {_id}=req.body
  await orderModel.updateOne({_id},req.body)
  res.send({
    code:200,
    msg:'订单修改成功'
  })
})
//获取个人信息表
router.get('/userinfo',async(req,res)=>{
  let data=await userinfoModel.find()
  res.send({
    code:200,
    msg:'个人信息表展示成功',
    data
  })
})


//获取评价表
router.get('/comment',async(req,res)=>{
  let data=await evaluateModel.find().populate('Admissions')
  res.send({
    code:200,
    msg:'评价表展示成功',
    data
  })
})

//获取患者信息表
router.get('/patientinfo',async(req,res)=>{
  let data=await AdmissionsModel.find()
  res.send({
    code:200,
    msg:'患者信息表展示成功',
    data
  })
})
module.exports = router;
